﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using ART.Common;

namespace ART.Admini.Setting
{
    public partial class SchoolDicList : System.Web.UI.Page
    {

        private ART.BLL.B_SchoolDic bll = new ART.BLL.B_SchoolDic();
        private ART.BLL.B_dictionary bll_dic = new ART.BLL.B_dictionary();

        private string strWhere, SearchName;
        private int AreaId;

        protected void Page_Load(object sender, EventArgs e)
        {
            SearchName = ART.Common.LYRequest.GetString("SearchName", true);
            AreaId = LYRequest.GetInt("AreaId", 0);

            if (!IsPostBack)
            {
                InitData();
                BindList();
            }
        }
        private void InitData()
        {
            JBind.BindList(dropAreaId, bll_dic.GetList("parentid=3").Tables[0], "DictionaryName", "id");
            dropAreaId.Items.Insert(0, new ListItem("请选择", "0"));
            if (this.AreaId != 0)
            {
                string strArea = "";
                strArea = bll_dic.GetIDbyParent(AreaId, true);

                Model.M_dictionary model = bll_dic.GetModel(AreaId);
                if (model.ParentId == 0) //一级
                {
                    this.dropAreaId.SelectedValue = this.AreaId.ToString();
                }
                else if (bll_dic.GetModel(model.ParentId).ParentId == 0) //二级
                {
                    JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + model.ParentId).Tables[0], "DictionaryName", "id");
                    dropAreaId2.Items.Insert(0, new ListItem("请选择上级", "0"));
                    this.dropAreaId2.SelectedValue = model.Id.ToString();
                    this.dropAreaId.SelectedValue = model.ParentId.ToString();
                }
                else //三级
                {
                    int parentid = bll_dic.GetModel(model.ParentId).ParentId;
                    model = bll_dic.GetModel(parentid);//sanji

                    JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + parentid).Tables[0], "DictionaryName", "id");
                    dropAreaId2.Items.Insert(0, new ListItem("请选择上级", "0"));

                    JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + model.ParentId).Tables[0], "DictionaryName", "id");
                    dropAreaId3.Items.Insert(0, new ListItem("请选择上级", "0"));


                    this.dropAreaId2.SelectedValue = parentid.ToString();
                    this.dropAreaId.SelectedValue = bll_dic.GetModel(parentid).ParentId.ToString();
                    this.dropAreaId3.SelectedValue = model.Id.ToString();
                }
            }

        }
        private void BindList()
        {

            #region __________Where语句__________

            strWhere = "  1=1";
            if (SearchName != "")
            {
                strWhere += " and (SchoolName like '%" + SearchName + "%')";
            }
            if (AreaId != 0)
            {
                strWhere += " and AreaId in (" + bll_dic.GetIDbyParent(AreaId,true)+")";
            }
            #endregion

            rpt_List.PageLink = "SchoolDicList.aspx?Search=Yes&AreaId=" + this.AreaId+"&SearchName=" + Utils.UrlEncode(SearchName);
            rpt_List.PageSize = 20;
            rpt_List.CurrentPage = LYRequest.GetInt("page", 1);

            DataSet ds = BLL.GetListByPage.GetDataSet_NoCache("SchoolDic", "*", strWhere, "", "ID DESC", "ID", rpt_List.CurrentPage, rpt_List.PageSize, 1);

            rpt_List.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            JBind.BindList(rpt_List, ds.Tables[1]);
        }
        protected void dropAreaId_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId.SelectedValue != "0")
            {
                dropAreaId2.Items.Clear();
                JBind.BindList(dropAreaId2, bll_dic.GetList("parentid=" + this.dropAreaId.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId2.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }

        protected void dropAreaId2_SelectedIndexChanged(object sender, EventArgs p1)
        {
            if (this.dropAreaId2.SelectedValue != "0")
            {
                dropAreaId3.Items.Clear();
                JBind.BindList(dropAreaId3, bll_dic.GetList("parentid=" + this.dropAreaId2.SelectedValue).Tables[0], "DictionaryName", "id");
                dropAreaId3.Items.Insert(0, new ListItem("请选择", "0"));
            }
        }
        protected void btnSearch_Click(object sender, EventArgs p1)
        {
            StringBuilder builder = new StringBuilder("SchoolDicList.aspx?Search=Yes");
            if (this.dropAreaId.SelectedValue != "0")
            {
                if (!(this.dropAreaId2.SelectedValue != "0"))
                {
                    builder.Append("&AreaId=" + this.dropAreaId.SelectedValue);
                }
                else if (!(this.dropAreaId3.SelectedValue != "0"))
                {
                    builder.Append("&AreaId=" + this.dropAreaId2.SelectedValue);
                }
                else
                {
                    builder.Append("&AreaId=" + this.dropAreaId3.SelectedValue);
                }
            }

            if (!string.IsNullOrEmpty(this.txtSearchName.Text.Trim()))
            {
                builder.Append("&SearchName=" + this.txtSearchName.Text.Trim());
            }
            base.Response.Redirect(builder.ToString());
        }
        protected String GetArea(object aid)
        {
            return bll_dic.GetString(Utils.StrToInt(aid, 0), true);
        }
        protected void lbtnDeletes_Click(object sender, EventArgs p1)
        {
#warning BLL.B_Adm.IsPermissions(0x6c)
            if (BLL.B_TBAdmin.IsPermissions(0x6c))
            {
                //int num = 0;
                foreach (RepeaterItem item in this.rpt_List.Items)
                {
                    HtmlInputCheckBox box = (HtmlInputCheckBox)item.FindControl("cheId");
                    if (box.Checked)
                    {
                        bll.Delete(int.Parse(box.Value));
                        // num++;
                    }
                }
                base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
            }
            else
            {
                MessageBox.Alert(sender, "没有删除权限!");
            }
        }
        protected void rpt_List_ItemCommand(object source, RepeaterCommandEventArgs p1)
        {
            int num = int.Parse(p1.CommandArgument.ToString());
            if (p1.CommandName == "lbtnDelete")
            {
#warning delte IsPermissions(0x6c)
                if (!BLL.B_TBAdmin.IsPermissions(0x6c))
                {
                    MessageBox.Alert(source, "没有删除权限!");
                }
                else
                {
                    bll.Delete(num);
                    base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
                }
            }
        }
    }
}
